<?php
// $Id: functions.php 98 2008-02-28 21:45:09Z mikhail.miguel $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //


/*
* Function to redirect a user to certain pages
*/
function message_header($url, $time = 3, $message = '', $goaction= true, $addredirect = true)
{

global $xoopsConfig, $xoopsRequestUri;

require_once XOOPS_ROOT_PATH.'/class/template.php';
$xoopsTpl = new XoopsTpl();
$xoopsTpl->xoops_setCaching(2);

$xoopsTpl->assign('time', $time);

    if (preg_match("/[\\0-\\31]/", $url) || preg_match("/^(javascript|vbscript|about):/i", $url)) {
        $url = XOOPS_URL;
    }
    else {
        if ($goaction) {
            if (!strstr($url, '?')) {
	    $url = str_replace(".php","",$url);
	    $url = 'javascript:SmartyAjax.update("content", "actions/'.$url.'_action.php", "get", "");';
            } else {
	    $urlarray = explode('?', $url);
	    $urlfile = str_replace(".php","", $urlarray[0]);
	    $url = 'javascript:SmartyAjax.update("content", "actions/'.$urlfile.'_action.php", "get", "'.$urlarray[1].'");';
            }
        }
       else {

        if ($addredirect && strstr($url, 'user.php')) {
            if (!strstr($url, '?')) {
                $url .= '?xoops_redirect='.urlencode($xoopsRequestUri);
            } else {
                $url .= '&amp;xoops_redirect='.urlencode($xoopsRequestUri);
            }
        }
        if (defined('SID') && (! isset($_COOKIE[session_name()]) || ($xoopsConfig['use_mysession'] && $xoopsConfig['session_name'] != '' && !isset($_COOKIE[$xoopsConfig['session_name']])))) {
            if (!strstr($url, '?')) {
                $url .= '?' . SID;
            }
            else {
                $url .= '&amp;'.SID;
            }
        }

       }
    }   
        $url = preg_replace("/&amp;/i", '&', htmlspecialchars($url, ENT_QUOTES));
        $xoopsTpl->assign('url', $url);
        $message = trim($message) != '' ? $message : _TAKINGBACK;
        $xoopsTpl->assign('message', $message);
        $xoopsTpl->assign('lang_ifnotreload', sprintf(_IFNOTRELOAD, $url));
        $GLOBALS['xoopsModuleUpdate'] = 1;

        if (!headers_sent()) {
            header('Content-Type:text/html; charset='._CHARSET);
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
            header('Cache-Control: no-store, no-cache, must-revalidate');
            header('Cache-Control: post-check=0, pre-check=0', false);
            header('Pragma: no-cache');
        }

        $xoopsTpl->xoops_setCaching(0);
        $xoopsTpl->display(XOOPSNEWSPATH.'/templates/news_redirect.html');
        exit();
}



/**
 * for mbstring array_convert_encoding by Tom_G3X
 * for iconv and mbstring array_convert_encoding by wanikoo
 **/
function ajax_array_convert_encoding($str, $to = _CHARSET , $from = 'UTF-8') 
{
	if (function_exists('iconv')) {
		if (is_array($str)) {
			foreach ($str as $key=>$val) {
				$str[$key] = ajax_array_convert_encoding($val, $to, $from);
			}
			return $str;
		} else {
			return @iconv($from, $to, $str);
		}
	}else {
		if (function_exists('mb_convert_encoding')) {
			if (is_array($str)) {
				foreach ($str as $key=>$val) {
					$str[$key] = ajax_array_convert_encoding($val, $to, $from);
				}
				return $str;
			} else {
				return @mb_convert_encoding($str, $to, $from);
			}
		}else {
			return $str;
		}
	}
} 



function ajax_convert_encoding($str, $to = _CHARSET , $from = 'UTF-8'){

if (function_exists('iconv')) {
		$str = is_string($str) ? @iconv($from, $to, $str) : $str;
		return $str;
} 
elseif (function_exists('mb_convert_encoding')) {
		$str = is_string($str) ? @mb_convert_encoding($str, $to, $from) : $str;
		return $str;
}
else
{
	return $str;
}

}

function newsOpenTable()
{
    echo "<table width='100%' border='0' cellspacing='1' cellpadding='8' style='border: 2px solid #2F5376;'><tr class='bg4'><td valign='top'>\n";
}

function newsCloseTable()
{
    echo '</td></tr></table>';
}

function newsthemecenterposts($title, $content)
{
    echo '<table cellpadding="4" cellspacing="1" width="98%" class="outer"><tr><td class="head">'.$title.'</td></tr><tr><td><br />'.$content.'<br /></td></tr></table>';
}


?>